<template>
  <div>
    <div class="header">
      <div class="title"><span>万卷书城</span></div>
      <div class="btns">
        <div class="list">
          <a href="javascript:;">链接按钮</a><a href="javascript:;">链接按钮</a>
        </div>
      </div>
    </div>
    <div class="container">

      <div class="left">
        <div class="login_title">注册</div>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="35%" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="昵称" prop="nick">
            <el-input v-model="ruleForm.nick"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-button style="width: 75%;margin-left:25% ;font-size: large" type="primary"
                     @click="submitForm('ruleForm')">注册
          </el-button>
        </el-form>
      </div>
      <div class="right">
        <div class="msg">
          <div>
            已有账号?
            <router-link to="/login">立即登录</router-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import router from "@/router";

export default {
  data() {
    let checkUserName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'));
      }
      setTimeout(() => {
        if (value.length > 20) {
          callback(new Error("用户名不能超过20个字符哦"))
        } else {
          axios.get("/user/username/" + value).then(
              r => {
                if (r.data.state === 40900) {
                  callback(new Error("用户名已存在,换一个吧"))
                }
              }
          ).catch(e => console.log(e))
          callback()
        }
      }, 1000);
    };
    let checkNick = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('昵称不能为空'));
      }
      setTimeout(() => {
        if (value.length > 20) {
          callback(new Error("昵称不能超过20个字符哦"))
        } else {
          callback()
        }
      }, 1000);
    };
    let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: '',
        nick: '',
        password: '',
      },
      rules: {
        username: [
          {validator: checkUserName, trigger: 'blur'}
        ],
        password: [
          {validator: validatePass, trigger: 'blur'}
        ],
        nick: [
          {validator: checkNick, trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.ruleForm)
          let data = {username:'',password:'',nick:''}
          data.username = this.ruleForm.username
          data.password = this.ruleForm.password
          data.nick = this.ruleForm.nick
          console.log('data: ')
          console.log(data)
          axios.post("/user/register",data).then(r=>{
            if (r.data.state===20000){
              this.$message.success("注册成功！")
              router.push('/login')
            }
            if (r.data.state === 50000){
              this.$message.warning(r.data.data)
            }
          }).catch(e=>console.log(e))
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>


<style scoped>
.msg {
  margin-left: 40px;
  font-size: 0.9rem;
  background-color: rgb(233, 233, 233);
  width: 100%;
  height: 40px;
  line-height: 40px;
  padding-left: 10px;
  border-radius: 5px;
}

.msg a {
  color: rgb(0, 119, 255);
}

.show {
  width: 300px;
  margin: 0 auto;
  text-align: center;
}

.show span {
  display: block;
  margin: 10px 0;
}

.show img {
  width: 100%;
  height: 100%;
}

.show :first-child {
  font-weight: bold;
  font-size: 1.2rem;
}

.show .back {
  border: 1px solid rgb(214, 214, 214);
  background-color: rgb(247, 247, 247);
  height: 30px;
  display: block;
  line-height: 30px;
  border-radius: 30px;
  cursor: pointer;
}

.show .back:hover {
  background-color: rgb(236, 236, 236);
}

ul {
  list-style: none;
}

a {
  text-decoration: none;
  color: gray;
}

input {
  outline: none;
  border: none;
  display: block;
}

.login {
  margin: 0 10px;
  font-family: "微软雅黑 light";
}

.header {
  border-top: 1px solid rgb(228, 228, 228);
  border-bottom: 1px solid rgb(228, 228, 228);
  height: 70px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  position: relative;
  width: 100%;
  background-color: rgb(247, 247, 247);
}

.title {
  color: rgb(129, 30, 30);
  font-size: 2.5rem;
  font-family: "方正舒体";
  font-weight: bold;
}

.btns {
  height: 100%;
  width: 140px;
}

.list {
  position: absolute;
  bottom: 0px;
  display: flex;
}

.btns a {
  display: block;
  text-decoration: none;
  color: gray;
  margin: 0 5px;
  font-size: 0.8rem;
  position: relative;
  transition: all 0.3s;
}

.btns a:hover {
  color: rgb(0, 174, 255);
  transform: scale(1.1);
  font-weight: bold;
  transition: all 0.3s;
}

.container {
  margin-top: 40px;
  display: flex;
}

.container > .left {
  width: 50%;
  border-right: 1px solid rgb(175, 175, 175);
  box-sizing: border-box;
  padding-right: 10%;
}

.container > .left > .content {
  margin: 0 auto;
  width: 300px;
}

.container > .left > .content > .login_header {
  display: flex;
  justify-content: space-between;
  height: 80px;
}

.container > .left > .content > .login_header > .avatar .hidden img {
  width: 50px;
  height: 50px;
  transform: rotate(-90deg);
  margin-top: 18px;
}

.container > .left > .content > .login_header > .avatar {
  position: relative;
  cursor: pointer;
}

.login_title {
  font-size: 2rem;
  font-weight: bold;
  margin: 0 0 10% 25%;
}

.container > .left > .content > .login_header > .avatar .avatar_login {
  position: absolute;
  top: 0;
  width: 100%;
  font-size: 0.7rem;
}

.user > .phone {
  text-align: right;
  font-size: 0.8rem;
  margin-bottom: 3px;
  margin-top: 20px;
}

.form {
  border: 1px solid gray;
  border-radius: 5px;
  overflow: hidden;
}

.form div {
  height: 35px;
}

.form div input {
  box-sizing: border-box;
  display: block;
  height: 100%;
  width: 100%;
  padding: 2px 10px;
}

.form div:first-child input {
  border-bottom: 1px solid rgb(163, 163, 163);
}

.checkbox {
  font-size: 0.8rem;
  line-height: 15px;
  display: flex;
  margin-top: 10px;
  margin-bottom: 30px;
  justify-content: space-between;
}

.checkbox div {
  display: flex;
  cursor: pointer;
  user-select: none;
}

.checkbox div input {
  width: 15px;
  height: 15px;
  margin-right: 3px;
}

label {
  display: flex;
}

.user button {
  width: 100%;
  height: 35px;
  background-color: rgb(32, 151, 248);
  color: white;
  border: none;
  border-radius: 4px;
  letter-spacing: 4px;
  margin-bottom: 30px;
}

.user button:active {
  font-size: 0.8rem;
  transform: translateY(1px);
  background-color: rgb(32, 198, 248);
}

.sLogin ul {
  border-top: 1px solid gainsboro;
  display: flex;
  justify-content: space-evenly;
  padding-top: 10px;
}

.sLogin li {
  display: flex;
  align-items: center;
  padding: 5px;
  border-radius: 5px;
}

.sLogin li:hover {
  cursor: pointer;
  background-color: rgb(206, 206, 206);
  color: white;
}

.sLogin li span {
  font-size: 0.8rem;
}

.icons {
  display: block;
  width: 20px;
  height: 20px;
  border-radius: 20px;
}

.right {
  width: 50%;
}
</style>